package com.yxw.live_vod_boot.controller;

import com.yxw.live_vod_boot.dto.*;
import com.yxw.live_vod_boot.service.LoginService;
import com.yxw.live_vod_boot.utils.RedisManager;
import com.yxw.live_vod_boot.vo.LoginUserVo;
import com.yxw.yxnet_cd_center.common.constant.RedisIdConstant;
import com.yxw.yxnet_cd_center.common.dto.BaseDTO;
import com.yxw.yxnet_cd_center.common.enums.CommonErrorEnum;
import com.yxw.yxnet_cd_center.common.enums.ErrorCodeEnum;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 *  直播平台登录注册管理
 * @author liuxin
 * @date 2023.11.29
 */
@CrossOrigin
@RestController
@Api(value = "直播平台登录注册管理", tags = "直播平台登录注册管理")
@Slf4j
@RequestMapping("/api/sys")
public class LoginController {

    @Autowired
    private LoginService  loginService;

    @Autowired
    private RedisManager redisManager;

    @PostMapping(value = "/auth/login")
    @ApiOperation(value = "外部接口登录授权")
    public BaseResult authLogin(@RequestBody @ApiParam(value = "认证加密信息") String token){
        return loginService.authLogin(token);
    }

    @ApiOperation(value = "账号密码登录")
    @PostMapping("/v1/login")
    public BaseResult<LoginUserVo> login(@Validated @RequestBody UserLoginDTO userLoginDTO) throws Exception {
        LoginUserVo loginUserVo = loginService.login(userLoginDTO);
        return BaseResult.doSuccess(loginUserVo,"登录成功");
    }

    /**
     * 手机号登录验证码获取
     */
    @ApiOperation(value = "手机号登录验证码获取")
    @PostMapping("/v1/send")
    public BaseResult send(@Validated @RequestBody UserSendCodeDTO userSendCodeDTO){
        String code=loginService.send(userSendCodeDTO);
        return BaseResult.doSuccess(code,"发送成功");
    }

    @ApiOperation(value = "密码找回验证码获取")
    @PostMapping("/v1/pwdSend")
    public BaseResult pwdSend(@Validated @RequestBody UserSendCodeDTO userSendCodeDTO){
        String code=loginService.pwdSend(userSendCodeDTO);
        return BaseResult.doSuccess(code,"发送成功");
    }

    @ApiOperation(value = "修改密码验证码校验")
    @PostMapping("/v1/verifyPwdSms")
    public BaseResult verifyPwdSms(@Valid @RequestBody VerifyPwdSmsDTO dto){
        String code = (String)redisManager.get(RedisIdConstant.UPDATE_PWD_SMS_CODE + dto.getPhone());
        if(!dto.getCode().equals(code)){
            throw new BaseException(CommonErrorEnum.SMS_VERIFICATION_CODE);
        }
        return BaseResult.doSuccess("","验证成功");
    }

    /**
     * 手机号验证码登录
     */
    @ApiOperation(value = "手机号验证码登录")
    @PostMapping("/v1/phone")
    public BaseResult<LoginUserVo> phone(@Validated @RequestBody UserPhoneLoginDTO userPhoneLoginDTO) {
        LoginUserVo loginUserVo = loginService.phone(userPhoneLoginDTO);
        return BaseResult.doSuccess(loginUserVo,"登录成功");
    }

    @ApiOperation(value = "系统登出")
    @PostMapping("/v1/logout")
    public BaseResult logout(@Validated @RequestBody BaseDTO baseDTO){
        loginService.logout(baseDTO);
        return BaseResult.doSuccess(null,"注销成功");
    }


    @ApiOperation(value = "刷新token接口")
    @PostMapping("/v1/refreshToken")
    public BaseResult<LoginUserVo> refreshToken(HttpServletRequest request)  {
        LoginUserVo loginUserVo=loginService.refreshToken(request);
        return BaseResult.doSuccess(loginUserVo,"刷新token成功");
    }

    @ApiOperation(value = "通过手机号修改密码")
    @PostMapping("/v1/updatePwdByPhone")
    public BaseResult updatePwdByPhone(@Validated @RequestBody UpdatePwdDTO dto){
        loginService.updatePwdByPhone(dto);
        return BaseResult.doSuccess("","修改成功");
    }
}
